package presentation.frame;

import java.awt.Color;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.rmi.RemoteException;
import java.util.ArrayList;

import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.KeyStroke;

import data.entity.*;
import presentation.redesign.*;

import rmiClient.Client;
import rmiClient.DownloadFile;

import bl.service.AccountService;
import bl.service.FtPrintsService;
import bl.service.JournalService;
import bl.service.PlanService;
import bl.service.SearchSpotService;
import bl.service.SearchUserService;
import javax.swing.JComboBox;

/**

 */
public class TitlePane extends JPanel {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JTextField searchText;
	Client c = new Client();
	UserEntity ue;

	@SuppressWarnings("serial")
	public TitlePane(UserEntity u) {
		this.ue = u;
		this.setBounds(0, 0, 1100, 80);
		setLayout(null);

		this.setBackground(new Color(160,191,124));//3,35,14
		


		Button mainButton = new Button("主页");

		mainButton.setForeground(Color.white);
		mainButton.addActionListener(new ShowMain(ue));
		mainButton.setBounds(128, 46, 76, 21);
		add(mainButton);

		Button destinationButton = new Button("目的地");
		destinationButton.setForeground(Color.white);
		destinationButton.addActionListener(new ShowRecommend(ue));
		destinationButton.setBounds(207, 46, 92, 20);
		add(destinationButton);

		Button tripButton = new Button("囧途");
		tripButton.setForeground(Color.white);
		tripButton.addActionListener(new ShowTrip());
		tripButton.setBounds(387, 46, 70, 21);
		add(tripButton);

		Button partnerButton = new Button("同伴");
		partnerButton.setForeground(Color.white);
		partnerButton.addActionListener(new ShowPartner());
		partnerButton.setBounds(302, 46, 83, 21);
		add(partnerButton);

		Label userNameLabel = new Label(ue.getName());
		userNameLabel.setForeground(Color.white);
		userNameLabel.setBounds(114, 9, 76, 21);
		add(userNameLabel);

		Button messageButton = new Button("消息");
		messageButton.setForeground(Color.white);
		messageButton.addActionListener(new ShowMessage(ue));
		messageButton.setBounds(456, 46, 92, 21);
		add(messageButton);

		Button exitButton = new Button("|  退出");
		exitButton.setForeground(Color.white);
		exitButton.addActionListener(new Exit(ue));
		exitButton.setBounds(1004, 10, 86, 21);
		add(exitButton);

		searchText = new JTextField();
		searchText.setBounds(880, 41, 108, 21);
		add(searchText);
		searchText.setColumns(10);

		String[] values = new String[] { "搜索景点", "搜索城市", "搜索游记", "搜索计划" };

		final JComboBox comboBox = new JComboBox(values);
		comboBox.setBounds(753, 41, 117, 21);		
		comboBox.setBackground(Color.white);		
		add(comboBox);

		final Button sureButton = new Button("搜索一下");
		sureButton.setForeground(Color.white);
	        KeyAdapter confirmAdapter = new KeyAdapter() {
				@Override
				public void keyPressed(KeyEvent arg0) {
					if (arg0.getKeyCode() == KeyEvent.VK_ENTER) {
						int index=comboBox.getSelectedIndex();
						String text=searchText.getText();
						search(index, text);
					}
				}
			};
	        
			searchText.addKeyListener(confirmAdapter);
			
		sureButton.addActionListener(new ActionListener(){
				@Override
				public void actionPerformed(ActionEvent arg0) {
					int index=comboBox.getSelectedIndex();
					String text=searchText.getText();
					// TODO Auto-generated method stub
					search(index,text);
				}
		});
		
		sureButton.setBounds(990, 40, 100, 23);
		add(sureButton);
		DownloadFile dlf=new DownloadFile();
		
		ImageIcon icon=new ImageIcon(dlf.download(ue.getPhoto(), "D://client"+ue.getPhoto()));
		Image image=icon.getImage();
		Image newImage=image.getScaledInstance(98, 63,Image.SCALE_SMOOTH);
		icon=new ImageIcon(newImage);
		JLabel photo = new JLabel(icon);
		photo.setBounds(6, 4, 98, 63);
		add(photo);
		String[]account={"个人主页","修改信息","修改密码"};
		final JComboBox accountBox = new JComboBox(account);
		accountBox.setBackground(Color.white);
		accountBox.addActionListener(new ActionListener(){
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				int index=accountBox.getSelectedIndex();
				switch(index){
				case 0:
					Frame.removePane();
					Frame.addPane(new UserPane(ue));
					break;
				case 1:
					Frame.removePane();
					Frame.addPane(new ModifyUserPane(ue));
					break;
				case 2:
					Frame.removePane();
					Frame.addPane(new ChangePasswordPane(ue));
					break;
				/*case 3:
					Frame.removePane();
					Frame.addPane(new DeleteUserPane(ue));
					break;*/
				}
			}
			
		});
		accountBox.setBounds(892, 10, 102, 21);
		add(accountBox);
	}

	class ShowMain implements ActionListener {

		public ShowMain(UserEntity ue) {
			// TODO Auto-generated constructor stub
		}

		@Override
		public void actionPerformed(ActionEvent arg0) {
			// TODO Auto-generated method stub
			Frame.removePane();
			Frame.addPane(new MainPane(ue));
		}

	}

	class ShowRecommend implements ActionListener {

		public ShowRecommend(UserEntity ue) {
			// TODO Auto-generated constructor stub
		}

		@Override
		public void actionPerformed(ActionEvent arg0) {
			// TODO Auto-generated method stub
			Frame.removePane();
			try {
				Frame.addPane(new RecommendPane(ue));
			} catch (RemoteException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}

	class ShowTrip implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			// TODO Auto-generated method stub
			Frame.removePane();
			ArrayList<JournalEntity> journal=new ArrayList<JournalEntity>();
			Client c=new Client();
			JournalService js=c.getJournalService();
			try {
				journal=js.getAll();
			} catch (RemoteException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				Frame.addPane(new JournalListPane(journal,ue));
			} catch (RemoteException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}

	class ShowPartner implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			// TODO Auto-generated method stub
			Frame.removePane();
			Client c=new Client();
			PlanService ps=c.getPlanService();
			ArrayList<PlanEntity> planList = new ArrayList<PlanEntity>() ;
			try {
				planList = ps.getAll();
				
			} catch (RemoteException e) {
				// TODO Auto-generateducatch block
				e.printStackTrace();
			}
			try {
				Frame.addPane(new PlanListPane(planList,ue));
			} catch (RemoteException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}

	class ShowMessage implements ActionListener {

		public ShowMessage(UserEntity ue) {
			// TODO Auto-generated constructor stub
		}

		@Override
		public void actionPerformed(ActionEvent arg0) {
			// TODO Auto-generated method stub
			Frame.removePane();
			Frame.addPane(new MsgAlertPane(ue));
		}

	}

	class Exit implements ActionListener {

		public Exit(UserEntity ue) {
			// TODO Auto-generated constructor stub
		}

		@Override
		public void actionPerformed(ActionEvent arg0) {
			// TODO Auto-generated method stub
			int answer=JOptionPane.showConfirmDialog(null, "确认退出吗？");
			if(answer==0){
				System.exit(0);
			}
		}

	}
	
	
	class SearchNullPane extends Panel {

		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;

		/**
		 * Create the panel.
		 */
		public SearchNullPane() {

			Label nullLabel = new Label("404 NOT FOUND please try it again...orz...");
			nullLabel.setBounds(10, 10, 400, 35);
			add(nullLabel);

		}
	}
	
	public void search(int index,String text){
		if (text.equals("")) {

			searchText.setText("您的输入为空，请重新输入");

			return;
		}
		switch (index){
		case 0:
			SearchSpotService sss=c.getSearchSpotService();
		try {
			if(sss.isSpotExist(text)){
				SpotEntity se=sss.searchSpot(text);
				Frame.removePane();
				Frame.addPane(new SpotIntroPane(se,ue));
			}else{
				Frame.removePane();
				Frame.addPane(new SearchNullPane());
			}
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}break;
		case 1:
			SearchSpotService ss=c.getSearchSpotService();
			try {
				if(ss.isCityExist(text)){
					CityEntity ce=ss.searchCity(text);
					Frame.removePane();
					Frame.addPane(new CityIntroPane(ce,ue));
				}else{
					Frame.removePane();
					Frame.addPane(new SearchNullPane());
				}
			} catch (RemoteException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}break;
		case 2:
			FtPrintsService fts=c.getFtPrintsService();
		try {
			ArrayList<Integer> journalList=fts.findCity(text);
			if(journalList.size()!=0){
				ArrayList<JournalEntity> journal=new ArrayList<JournalEntity>();
				for(int i=0;i<journalList.size();i++){
					JournalService js=c.getJournalService();
					JournalEntity je=js.getByUser(ue.getId()).get(0);
					journal.add(je);
				}
				Frame.removePane();
				Frame.addPane(new JournalListPane(journal,ue));
				
			}else{
				Frame.removePane();
				Frame.addPane(new SearchNullPane());
			}
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}break;
		case 3:
			SearchUserService searchUS = c.getSearchUserService();
			SearchSpotService searchSS = c.getSearchSpotService();
			System.out.println(text);
			try {
				if(text.equals(null)){
					Frame.removePane();
					Frame.addPane(new SearchNullPane());
				}else{
					ArrayList<PlanEntity> planList = new ArrayList<PlanEntity>();
					if(searchSS.isCityExist(text)){
						planList = searchUS.getPlanList(text);
					}else{
						JOptionPane.showConfirmDialog(null, "该地点不存在");
					}
					Frame.removePane();
					Frame.addPane(new SearchUsrPane(ue, planList));
				}						
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
	}
	}
}
